﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.Data;

using System.Windows;
using System.Windows.Data;

using System.Windows.Controls;

namespace HA.ModFlow.Application
{
    public class ProjectViewModel : MDTVIViewModel
    {

        public ProjectViewModel()
        {
            this.Init();

           

        }





        Project _project;


        TimeStepViewModel _tsvViewModel;

        public TimeStepViewModel TsvViewModel
        {
            get { return _tsvViewModel; }
            set { _tsvViewModel = value; }
        }


    


        protected void Init()
        {
            this.LoadChildren();


            this._tsvViewModel = new TimeStepViewModel();

 
        }

        public override string Desc
        {
            get
            {
                return "22";
            }
        }

        protected override void LoadChildren()
        {
            if (ProjectService.DefaultService.BNullProj)
	        {
                        return;
		 
	        }

         


            ProjectDataViewModel pd = new ProjectDataViewModel(this);
            this.Children.Add(pd);

        }


        /// <summary>
        /// 取得所属
        /// </summary>
        /// <param name="colletion"></param>
        /// <returns></returns>
        public DSGrid3dViewModel GetFirstDSGridVM(DSGrid3dCollecion colletion)
        {

            ProjectDataViewModel pd = (ProjectDataViewModel)this.Children[0];

            Grid3dCollecionViewModel vm= pd.Get3DCollecionVM();

            Grid3dViewModel grid= vm.GetMainGrid3dVM();

            if (grid==null)
            {
                return null;
                
            }


            DSGrid3dCollecionViewModel dsCollection= grid.GetDSCollecion(colletion);

            if (dsCollection==null)
            {
                return null;
                
            }

            if (dsCollection.Children.Count > 0)
            {
                return (DSGrid3dViewModel)dsCollection.Children[0];

            }

            else
            {
                return null;
            }
           



 
        }


        public void SetFirstDSSelected(DSGrid3dCollecion colletion)
        {

            SetAllDSLoseSelected();


            DSGrid3dViewModel vm = this.GetFirstDSGridVM(colletion);

            if (vm!=null)
            {
                vm.IsSelected = true;
                
            }
 
        }

        /// <summary>
        /// 
        /// 设置所有的DSGrid3dViewModel和DSVectorGrid3dViewModel的selected为false
        /// 
        /// 原因：当调用上述SetFirstDSSelected 方法后，会首先设置firstDSGrid的selected为true，TimeStepViewModel对应的窗体显示
        /// 之后，才会导致在上述过程之前所选择的selected为false，这将导致一种错误：当前选择显示Head数据中的某一个cellCenter数据，
        /// 计算MT3d，读取完结果后，会调用上述方法，从而设置mt3d对应的数据选择为true，之后会设置Head选择为false，从而导致TimeStepViewModel
        /// 对应的窗体被隐藏掉，关键点就是上述selected改变的顺序导致问题，所以在上述方法执行之前，调用该方法设置所有的数据选择为false
        /// 再执行setFirstDSGrid。
        /// 
        /// </summary>
        protected void SetAllDSLoseSelected()
        {

            try
            {


                ProjectDataViewModel pd = (ProjectDataViewModel)this.Children[0];

                Grid3dCollecionViewModel vm = pd.Get3DCollecionVM();

                Grid3dViewModel grid = vm.GetMainGrid3dVM();

                if (grid == null)
                {
                    return;
                }

                foreach (MDTVIViewModel item in grid.Children)
                {
                    if (item is DSGrid3dCollecionViewModel)
                    {
                        DSGrid3dCollecionViewModel vmdsc = (DSGrid3dCollecionViewModel)item;

                        if (vmdsc != null)
                        {

                            foreach (MDTVIViewModel item2 in vmdsc.Children)
                            {
                                if (item2 is DSGrid3dViewModel)
                                {
                                    DSGrid3dViewModel vmds = (DSGrid3dViewModel)item2;

                                    if (vmds != null)
                                    {
                                        vmds.IsSelected = false;
                                    }

                                }
                                if (item2 is DSVectorGrid3dViewModel )
                                {
                                    DSVectorGrid3dViewModel vmds = (DSVectorGrid3dViewModel)item2;

                                    if (vmds != null)
                                    {
                                        vmds.IsSelected = false;
                                    }

                                }


                            }

                        }

                    }
                }
            }
            catch
            {
                return;
            }


        }



    }
}
